#include "data_build.h"

#include <string>
#include "tool.h"
#include "soaC2SignalToEdc.h"

DataBuild::DataBuild(/* args */)
{
}

DataBuild::~DataBuild()
{
}

// R8 new

void DataBuild::DataCollectUpload_send(IN const string _Evtname, IN const uint64_t timestamp, IN Json::Value &param)
{
    Json::Value root;
    Json::Value header;
    Json::Value data;

    std::string _m = "";
    _m = _m + "Dat_" + _Evtname;

    //得到请求ID
    std::string reqId = "";
    getReqID(reqId);

    header["v"] = Json::Value("1.0.0");
    header["r"] = Json::Value(reqId);
    header["sk"] = Json::Value("Dat_CarData");
    header["m"] = Json::Value(_m);
    header["a"] = Json::Value(0);
    header["e"] = Json::Value(0);

    param["ts"] = timestamp;
    param["ep"] = 2;
    param["ec"] = Json::Value(_m);
    data["p"] = param;
    root["h"] = header;
    root["d"] = data;

    std::string sendStr = root.toStyledString();
    Log_Info("push json: {}", sendStr);
    mqttsend(sendStr);
}

void DataBuild::DataCollectUpload_send(const Soa::PB_DC_FileList_Struct &fileList, const std::string &eventCode)
{
    Log_Info("RPC receive trigger: {}", eventCode);
    Json::Value param;
    param["ec"] = eventCode;
    uint64_t time = CommonOprTime::getTime();
    param["ut"] = time;
    param["ts"] = time;
    param["ep"] = "1";
    param["Lng"] = global_data::g_car_location_lng;
    param["Lat"] = global_data::g_car_location_lat;

    Json::Value fileArray;
    for (int i = 0; i < fileList.seq_size(); i++)
    {
        Json::Value fileJson;
        Soa::PB_DC_FileListStr file = fileList.seq(i);
        fileJson["controllerCode"] = global_data::g_controler_code;
        fileJson["fileName"] = file.filename(); 
        fileJson["filePath"] = file.filepath();
        fileArray.append(fileJson);
        Log_Info("RPC receive trigger fileName: {}  filePath: {}", file.filename(), file.filepath());
    }
    param["fileCtx"] = fileArray;

    DataBuild::DataCollectUpload_send("Et_CarData", "Et_EventFile", param);
}

void DataBuild::DataCollectUpload_send(IN const std::string _Evtname, IN Json::Value &param)
{
    uint64_t timestamp = CommonOprTime::getTime();
    DataCollectUpload_send(_Evtname, timestamp, param);
}

void DataBuild::DataCollectUpload_send(IN const std::string _sk, IN const std::string _m, IN Json::Value &param)
{
    Json::Value root;
    Json::Value header;
    Json::Value data;

    //得到请求ID
    std::string reqId = "";
    CommonOprServser::getReqID(reqId);

    header["v"] = Json::Value("1.0.0");
    header["r"] = Json::Value(reqId); // 1463106055567 time ms
    header["sk"] = Json::Value(_sk);
    header["m"] = Json::Value(_m);
    header["a"] = Json::Value(1); //
    header["e"] = Json::Value(0); //

    param["ts"] = CommonOprTime::getTime();
    data["p"] = param;
    root["h"] = header;
    root["d"] = data;

    std::string sendStr = root.toStyledString();

    mqttsend(sendStr);
}
void DataBuild::mqttsend(const string &sendStr)
{
    // EDC公有mqtt通道
    // 去DDS
    SoaC2SignalToEdc::getInstance().sendEvent(sendStr);
    // DdsC2SignalToEdc::getInstance().sendEvent(sendStr);
}

void DataBuild::DataCollectSignal_send(IN const std::string _sk, IN const std::string _m, IN Json::Value &param)
{
    Json::Value root;
    Json::Value header;
    Json::Value data;

    //得到请求ID
    std::string reqId = "";
    CommonOprServser::getReqID(reqId);

    header["v"] = Json::Value("1.0.0");
    header["r"] = Json::Value(reqId); // 1463106055567 time ms
    header["sk"] = Json::Value(_sk);
    header["m"] = Json::Value(_m);
    header["a"] = Json::Value(1); //
    header["e"] = Json::Value(0); //

    param["ts"] = CommonOprTime::getTime();
    data["p"] = param;
    root["h"] = header;
    root["d"] = data;

    std::string sendStr = root.toStyledString();
    SoaC2SignalToEdc::getInstance().sendSignalToMqtt(sendStr);
}
